package sdu.icat.pj.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sdu.icat.pj.repository.SysNoticeRepository;
import sdu.icat.sys.smodel.SysNotice;

/**
 * 系统公告
 * @author ljh_2015
 *
 */
@Service
public class SysNoticeService {

    @Autowired private SysNoticeRepository noticeRepository;

    /**
     * 新增或更新
     * @param notice
     * @return
     */
    @Transactional
    public SysNotice save(SysNotice notice) {
	return noticeRepository.save(notice);
    }

    @Transactional
    public void remove(Long[] ids) {
	for(int i=0;i<ids.length;i++) {
	    noticeRepository.delete(ids[i]);
	}
    }

    @Transactional
    public void top(SysNotice newnotice) {
	List<SysNotice> notices = noticeRepository.findByIsShow(true);
	for(SysNotice notice:notices) {
	    notice.setShow(false);
	    noticeRepository.save(notice);
	}
	newnotice.setShow(true);
	save(newnotice);
    }

    @Transactional
    public SysNotice currentShow() {
	List<SysNotice> notices = noticeRepository.findByIsShow(true);
	return notices.get(0);
    }
}
